//
//  Car_ContrastCarTableViewCell.m
//  CarUser
//
//  Created by ayi on 2019/1/17.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "Car_ContrastCarTableViewCell.h"

@interface Car_ContrastCarTableViewCell()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIImageView *logoImgv;
@property (nonatomic, strong) UILabel *tipsLab;
@property (nonatomic, strong) UILabel *moneyLab;

@end

@implementation Car_ContrastCarTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (Car_ContrastCarTableViewCell *)initCellWithReuseIdentifier:(NSString *)reuseIdentifier andTab:(UITableView *)tab{
    Car_ContrastCarTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[Car_ContrastCarTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _logoImgv = [[UIImageView alloc]init];
        _titleLab = [UILabel customInitLabWithFont:14 andTextColor:Common_TextColor andAlign:NSTextAlignmentLeft];
        _titleLab.numberOfLines = 2;
        _tipsLab = [UILabel customInitLabWithFont:14 andTextColor:Common_TextColor andAlign:NSTextAlignmentLeft];
        _moneyLab = [UILabel customInitLabWithFont:14 andTextColor:Common_TextColor andAlign:NSTextAlignmentLeft];
        _chooseBtn = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.userInteractionEnabled = NO;
            [btn setImage:IMG(@"ic_unselect") forState:UIControlStateNormal];
            [btn setImage:IMG(@"ic_select") forState:UIControlStateSelected];
            btn;
        });
        UIView *bgview = [[UIView alloc]init];bgview.backgroundColor = UIColor.whiteColor;
        bgview.layer.masksToBounds = YES;
        bgview.layer.cornerRadius = 8;
        
        [self.contentView addSubview:bgview];
        
        [bgview addSubview:_logoImgv];
        [bgview addSubview:_titleLab];
        [bgview addSubview:_tipsLab];
        [bgview addSubview:_moneyLab];
        [bgview addSubview:_chooseBtn];
        
        [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
        }];
        
        [_logoImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            make.left.mas_equalTo(10);
            make.bottom.mas_equalTo(-10);
            make.height.mas_equalTo(ceilf(SCALE_HEIGHT(100)));
            make.width.mas_equalTo(ceilf(SCALE_WIDTH(140)));
        }];
        
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.logoImgv.mas_top).offset(2);
            make.left.equalTo(self.logoImgv.mas_right).mas_equalTo(8);
            make.right.mas_equalTo(-10);
        }];
        
        
        [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.logoImgv.mas_bottom);
            make.left.equalTo(self.logoImgv.mas_right).mas_equalTo(8);
            make.height.mas_equalTo(ceilf(self.moneyLab.font.lineHeight));
            make.right.equalTo(self.chooseBtn.mas_left).offset(10);
        }];
        
        [_chooseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-4);
            make.centerY.equalTo(self.moneyLab.mas_centerY);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(30);
        }];
        
        [_tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.moneyLab.mas_top).offset(-ceilf(SCALE_HEIGHT(17)));
            make.left.equalTo(self.logoImgv.mas_right).mas_equalTo(8);
            make.right.equalTo(self.chooseBtn.mas_left).offset(10);
            make.height.mas_equalTo(ceilf(self.tipsLab.font.lineHeight) + 4);
        }];
    }
    return self;
}

- (void)setModel:(CarDetailMainModel *)model{
    _model = model;
    
    @weakify(self);
    [self.logoImgv sd_setImageWithURL:[NSURL URLWithString:model.cardetailInfoModel.ZST000] placeholderImage:[UIImage getDefaultPersonLogoImage] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        @strongify(self);
        if (image && cacheType == SDImageCacheTypeNone) {
            [self.logoImgv.layer addCustomImageViewLayerAnimation];
        }
    }];
    
    self.titleLab.text = model.cardetailInfoModel.CXMC00;
    [self resetTipsLabText];
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@万",model.cardetailInfoModel.ZDJG00];
    
    self.chooseBtn.selected = model.hasChoose;
}



//标签
- (void)resetTipsLabText{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]init];
    [str appendAttributedString:[UIView imgAttirbuteStrWith:self.model.cardetailInfoModel.GHFSMC?:@""]];
    [str appendAttributedString:[[NSMutableAttributedString alloc]initWithString:@"   "]];
    [str appendAttributedString:[UIView imgAttirbuteStrWith:self.model.cardetailInfoModel.RLMC00?:@""]];
    
    self.tipsLab.attributedText = str;
}


@end
